package cn.e2win.biz.cyzj;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import cn.e2win.base.entity.BaseEntity;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;
import cn.e2win.biz.base.CommonStatic;
import cn.e2win.biz.dbo.Activity;
import cn.e2win.biz.dbo.CyzjEmp;
import cn.e2win.biz.dbo.CyzjOrg;
import cn.e2win.biz.util.StringUtils;


@Repository
public class CyzjDaoImpl extends BaseDAOHibernateImpl implements CyzjDao {

	String entitySql_CyzjEmp =  "SELECT t.* FROM("
			+ "    SELECT t0.*, t1.orgName "
			+ "    FROM cyzj_emp t0, cyzj_org t1 "
			+ "    WHERE t0.orgId = t1.id"
			+ " ) t WHERE 1=1";
	
	String entitySql_CyzjOrg = "SELECT t.* FROM("
			
			+ " SELECT t0.*"
			+ " ,t1.label AS labelMain "
			+ " ,t2.label AS labelOffice "
			+ " ,t3.dsEmpIds AS dsEmpIds "
			+ " ,t3.dsEmpNames AS dsEmpNames "
			+ " FROM cyzj_org t0 "
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='CyzjOrg' AND t1.labelType='labelMain' "
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='CyzjOrg' AND t2.labelType='labelOffice' "
			+ " LEFT JOIN ("+CommonStatic.dsRelationSql+") AS t3 ON t0.id = t3.entityId AND t3.entityType='CyzjOrg'"
			
			+ " ) t "
			
			+ " WHERE 1=1 ";
	

	@Override
	public void pagerCyzj(Pager pager) {
		try {
			String weixinX=pager.getScMap().get("x_I");
			String weixinY=pager.getScMap().get("y_I");
			if("RECENT".equals(pager.getScMap().get("near_I"))){
				pager.setOrderField("distance");
				pager.setOrderDirection("ASC");
			}
			String sql = "SELECT t.* FROM("
					
					+ " SELECT t0.* "
					+ " ,"+weixinX+" AS weixinX"
					+ " ,"+weixinY+" AS weixinY"
					+  CommonStatic.getDistanceSql(weixinX, weixinY, " t0.x", " t0.y", "distance", "distanceKm") 
					+ " ,t1.label AS labelMain "
					+ " ,t2.label AS labelOffice "
					+ " ,t3.dsEmpIds AS dsEmpIds "
					+ " ,t3.dsEmpNames AS dsEmpNames "
					+ " FROM cyzj_org t0 "
					+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='CyzjOrg' AND t1.labelType='labelMain' "
					+ " LEFT JOIN ("+CommonStatic.labSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='CyzjOrg' AND t2.labelType='labelOffice' "
					+ " LEFT JOIN ("+CommonStatic.dsRelationSql+") AS t3 ON t0.id = t3.entityId AND t3.entityType='CyzjOrg'"
					
					+ " ) t "
					
					+ " WHERE 1=1 ";
			String user_I = pager.getScMap().get("user_I");
			SysUser user = pager.getCurrUser();
//			System.err.println("usersession.getUser===" + user.getId() + "===" + user.getEmpId());
			if(!StringUtils.isEmpty(user_I) && user.getIsLeader()!="1"){
				if(user_I.equals("master")){
					sql += " And dsEmpId="+user.getEmpId()+" ";
				}else if(user_I.equals("join")){
					sql += " And dsEmpNames like '%"+user.getUserName()+"%' ";
				}else if(user_I.equals("admin")){
					sql += " And (adminUserId="+user.getId()+" or adminUserId2="+user.getId()+") ";
				}
			}
			String adminUserId = pager.getScMap().get("adminUserId_I");
			if(!StringUtils.isEmpty(adminUserId)){
				sql += "And (adminUserId="+adminUserId+" or adminUserId2="+adminUserId+") ";
			}

			//用于搜索
			String title_I = pager.getScMap().get("title_I");
			if(!StringUtils.isEmpty(title_I)){
				sql += " And (orgName like '%"+title_I+"%' or address like '%"+title_I+"%' ) ";
			}

			pager.processSql(sql + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void pagerDsRelatedCyzj(Pager pager) {
		try {
			String sql = entitySql_CyzjOrg;
			String user_I = pager.getScMap().get("user_I");
			SysUser user = pager.getCurrUser();
//			System.err.println("usersession.getUser===" + user.getId() + "===" + user.getEmpId());
			if(!StringUtils.isEmpty(user_I) && user.getIsLeader()!="1"){
				if(user_I.equals("master")){
					sql += " And dsEmpId="+user.getEmpId()+" ";
				}else if(user_I.equals("join")){
					sql += " And dsEmpNames like '%"+user.getUserName()+"%' ";
				}else if(user_I.equals("admin")){
					sql += " And (adminUserId="+user.getId()+" or adminUserId2="+user.getId()+") ";
				}
			}
			String adminUserId = pager.getScMap().get("adminUserId_I");
			if(!StringUtils.isEmpty(adminUserId)){
				sql += "And (adminUserId="+adminUserId+" or adminUserId2="+adminUserId+") ";
			}

			//用于搜索
			String title_I = pager.getScMap().get("title_I");
			if(!StringUtils.isEmpty(title_I)){
				sql += " And (orgName like '%"+title_I+"%' or address like '%"+title_I+"%' ) ";
			}
			
			//导师
			String dsEmpIds_I = pager.getScMap().get("dsEmpIds_I");
			if(!StringUtils.isEmpty(dsEmpIds_I)){
				sql += " And (dsEmpIds like '%|"+dsEmpIds_I+"|%' or dsEmpId ="+dsEmpIds_I+" ) ";
			}
			
			
			

			pager.processSql(sql + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void pagerCyzjEmp(Pager pager) {
		try {
			pager.processSql(entitySql_CyzjEmp + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Map<String, Object>getEntity(BaseEntity entity){
		String sql = null;
		if(entity instanceof CyzjEmp){
			sql = entitySql_CyzjEmp;
		}else if(entity instanceof CyzjOrg){
			sql = entitySql_CyzjOrg;
		}
		if(sql!=null){
			return super.get1stRecordBySql(sql + " AND id=" + entity.getId(), null);
		}
		return null;
	}
	
	@Override
	public Map<String, Object>getCyzjOrgDetail(Integer id, Map<String, String> param){
		String sql = "SELECT t.* FROM("
				
				+ " SELECT t0.*"
				+ " ,t1.label AS labelMain "
				+ " ,t2.label AS labelOffice "
				+ " ,t3.dsEmpIds AS dsEmpIds "
				+ " ,t3.dsEmpNames AS dsEmpNames "
				+  CommonStatic.getDistanceSql(param.get("x_I"), param.get("y_I"), " t0.x", " t0.y", "distance", "distanceKm") 
				+ " FROM cyzj_org t0 "
				+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='CyzjOrg' AND t1.labelType='labelMain' "
				+ " LEFT JOIN ("+CommonStatic.labSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='CyzjOrg' AND t2.labelType='labelOffice' "
				+ " LEFT JOIN ("+CommonStatic.dsRelationSql+") AS t3 ON t0.id = t3.entityId AND t3.entityType='CyzjOrg'"
				
				+ " ) t "
				
				+ " WHERE 1=1 "
				+ " AND id=" + id;
		return super.get1stRecordBySql(sql , param);
	}

	@Override
	public void updateAdmin(CyzjOrg cyzjOrg) {
		List<Activity> list =(List<Activity>)super.getObjectListByHql("FROM Activity WHERE orgId="+ cyzjOrg.getId());
		for(Activity activity:list){
			activity.setAdminUserId(cyzjOrg.getAdminUserId());
			activity.setAdminUserId2(cyzjOrg.getAdminUserId2());
		}
		super.saveAll(list);
	}

}
